package com.tianhui.consignortwofei.mvp.ui.fragment;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.fgs.common.CommonResponse;
import com.fgs.common.other.CommonEvent;
import com.fgs.common.progress.ExceptionHandle;
import com.fgs.common.progress.ObserverResponseListener;
import com.tianhui.consignortwofei.R;
import com.tianhui.consignortwofei.constant.KeyValue;
import com.tianhui.consignortwofei.manager.EventBusManage;
import com.tianhui.consignortwofei.mvp.contract.CarrierContract;
import com.tianhui.consignortwofei.mvp.model.DriverModel;
import com.tianhui.consignortwofei.mvp.model.enty.CarrierInfo;
import com.tianhui.consignortwofei.mvp.model.enty.DriverInfo;
import com.tianhui.consignortwofei.mvp.presenter.CarrierPresenter;
import com.tianhui.consignortwofei.mvp.ui.activity.carrier.AddCarrierActivity;
import com.tianhui.consignortwofei.mvp.ui.activity.carrier.EditCarrierActivity;
import com.tianhui.consignortwofei.mvp.ui.adapter.CarrierListAdapter;
import com.tianhui.consignortwofei.other.EventWhat;

import org.yczbj.ycrefreshviewlib.adapter.RecyclerArrayAdapter;
import org.yczbj.ycrefreshviewlib.inter.OnItemChildClickListener;
import org.yczbj.ycrefreshviewlib.inter.OnItemClickListener;

import java.util.HashMap;
import java.util.Map;

import butterknife.OnClick;

/**
 * @author fgs
 * @date 2021/5/22
 * @description
 **/
public class CarrierListFragment extends SearchFragment<CarrierContract.View, CarrierContract.Presenter> implements CarrierContract.View {

    private String mSearchKey;
    private DriverInfo driverInfo;
    private DriverModel driverModel;

    public CarrierListFragment() {
    }

    @Override
    protected void changeSearchKey(String searchKey) {
        mSearchKey = searchKey;
    }

    @Override
    protected boolean isNeedLazyLoad() {
        return false;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_carrier_list;
    }

    @Override
    protected CarrierContract.View createView() {
        return this;
    }

    @Override
    protected CarrierContract.Presenter createPresenter() {
        return new CarrierPresenter(getContext());
    }


    @Override
    public RecyclerArrayAdapter createAdapter() {
        return new CarrierListAdapter(getContext());
    }

    @Override
    protected void initListener() {
        mAdapter.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(int position) {
                CarrierInfo carrierInfo = (CarrierInfo) mAdapter.getAllData().get(position);
                if (driverInfo != null && carrierInfo != null) {
                    driverInfo.carrier = carrierInfo.carrierno;
                    driverInfo.carriername = carrierInfo.carriername;
                    driverInfo.carriertype = carrierInfo.carrierno;
                    requestDriverInfoSave(driverInfo);

                }
            }
        });

        mAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(View view, int position) {
                CarrierInfo info;
                switch (view.getId()) {
                    case R.id.item_carrier_list_editButton:
                        info = (CarrierInfo) mAdapter.getAllData().get(position);
                        if (info != null) {
                            Bundle bundle = new Bundle();
                            bundle.putParcelable(KeyValue.KEY_CARRIER_INFO, info);
                            openActivity(EditCarrierActivity.class, bundle);
                        }
                        break;
                }
            }
        });

    }

    private void requestDriverInfoSave(DriverInfo driverInfo) {
        driverModel.driverInfoSaveModel(getContext(), driverInfo, true, this.bindLifecycle(), new ObserverResponseListener<CommonResponse>() {
            @Override
            public void onNext(CommonResponse commonResponse) {
                if (commonResponse.getCode() == 20000) {
                    EventBusManage.refreshDriverList();
                    getActivity().finish();
                } else {
                    showToast(commonResponse.getMsg());
                }

            }

            @Override
            public void onError(ExceptionHandle.ResponseThrowable e) {
                showToast(e.message);
            }
        });
    }

    @Override
    public void requestDataList(int page, boolean isDialog) {
        Map<String, String> map = new HashMap<>();
        map.put("limit", "10");
        map.put("page", page + "");
        if (!TextUtils.isEmpty(mSearchKey)) {
            map.put("carriername", mSearchKey);
        }
        getPresenter().requestCarrierList(map, isDialog);
    }

    @Override
    public void initData() {
        Bundle bundle = getArguments();

        if (bundle != null && !bundle.isEmpty()) {
            driverInfo = bundle.getParcelable(KeyValue.KEY_DRIVER_INFO);
            if (driverInfo != null) {
                mSearchKey = driverInfo.drivername;
                mSearchKeyEditText.setText(driverInfo.drivername);
            }
        }
        driverModel = new DriverModel();

        setSearchKeyHint("请输入承运商名称或身份证号");
    }


    @Override
    public void onEvent(CommonEvent event) {
        super.onEvent(event);
        switch (EventWhat.valueOf(event.getWhat())) {
            case EVENT_REFRESH_CARRIER_LIST:
                refreshData(true);
                break;
        }
    }

    @Override
    protected void requestSearch(String searchKey) {
        mSearchKey = searchKey;
        refreshData(true);
    }

    @OnClick(R.id.activity_carrier_list_addCarrierButton)
    public void addCarrier() {

        openActivity(AddCarrierActivity.class, getArguments());
    }


}
